package com.example.server;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.TaskResult;
import com.example.logic.GameController;
import com.example.util.GameModel;
import com.example.util.MethodName;

/**
 * The controller servlet of the 8-puzzle application, all requests should be directed to this servlet.
 */
public class ControllerServlet extends HttpServlet {
	 
	private boolean isInit = false;
	
    /**
     * Handles an http GET request
     */
    protected synchronized void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        GameController gameController = GameController.getInstance();
        if (isInit == false) {
        	Thread controllerThread = new Thread (gameController);   	
        	controllerThread.start();
        	isInit = true;
        }
        boolean isMobile = "mobile".equals(request.getParameter("userAgent"));
        GameModel model = null;
        int playerPoints ;
        int playerHitPoints;
        int enemyHitPoints;
        MethodName method = MethodName.valueOf(request.getParameter("method"));
        model = gameController.getModels();
        playerPoints = gameController.getPlayers()[0].getPoints();
        playerHitPoints = gameController.getPlayers()[0].getCentralBase().getHitPoints();
        enemyHitPoints = gameController.getPlayers()[2].getCentralBase().getHitPoints();
        String messageToDisplay = gameController.getMessageToDisplay();
        boolean isCanvasMoving = gameController.isCanvasMoving();
        float xCanvasMoveDestination = gameController.getxCanvasMoveDestination();
        float yCanvasMoveDestination = gameController.getyCanvasMoveDestination();
        if (method == null) {
        	System.out.println ("method is null");
        }
        else {
        	switch (method) {
	        	
        		case newModelCreation:
	        		int xTouchPosition = Integer.parseInt(request.getParameter("xTouchPosition"));
	        		int yTouchPosition = Integer.parseInt(request.getParameter("yTouchPosition"));
	        		gameController.onNewModelCreationRequest(xTouchPosition, yTouchPosition);
	        		model = gameController.getModels();
	        		break;
	        	
	        	case init:
	        		model = gameController.getModels();
	        		break;
	        	
	        	case gameExit:
	        		gameController.onGameExit();
	        		break;
        	}
        }
        
        TaskResult result = new TaskResult();
        result.setModel(model);
        result.setPlayerPoints(playerPoints);
        result.setPlayerHitPoints(playerHitPoints);
        result.setEnemyHitPoints(enemyHitPoints);
        result.setMessageToDisplay(messageToDisplay);
        result.setCanvasMoving(isCanvasMoving);
        result.setxCanvasMoveDestination(xCanvasMoveDestination);
        result.setyCanvasMoveDestination(yCanvasMoveDestination);
        request.setAttribute("result", result);
        String responsePath = isMobile ? "/mobileResponse" : "/webResponse";
        getServletContext().getRequestDispatcher(responsePath).forward(request, response);
        gameController.setMessageToDisplay("");
        gameController.setCanvasMoving(false);
    }
}
